﻿

using System.Linq;
using System;

namespace ZDK.Infra.Helpers
{
    public static class ByteHelper
    {
        #region U16ArrToByteArr ushort[]转byte[]
        public static byte[] U16ArrToByteArr(ushort[] src)
        {
            byte[] bt = new byte[2];
            byte[] ret = new byte[src.Length * 2];
            for (int i = 0; i < src.Length; ++i)
            {
                bt = BitConverter.GetBytes(src[i]);
                Array.Reverse(bt);
                Array.Copy(bt, 0, ret, i * 2, 2);
            }
            return ret;
        }
        #endregion

        #region ByteArrToU16Arr byte[]转ushort[]
        public static ushort[] ByteArrToU16Arr(byte[] src)
        {
            byte[] bt = new byte[2];
            ushort[] ret = new ushort[src.Length >> 1];
            for (int i = 0; i < ret.Length; ++i)
            {
                bt = src.Skip(i * 2).Take(2).ToArray();
                Array.Reverse(bt);
                ret[i] = BitConverter.ToUInt16(bt, 0);
            }
            return ret;
        }
        #endregion
    }
}
